<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ZlYearActiveTextChangeRecode extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_year_active_text_change_recode';

    protected $cacheModel = 'active.year_text';

    /**
     * 文字中奖概率
     * @var array
     */
    protected $textScale = [
        '2022' => 0.03,
        '虎' => 0.32,
        '气' => 0.31,
        '冲' => 0.31,
        '天' => 0.03,
    ];

    /**
     * 获取当天用户是否签到
     * @param int $activeFlag
     * @param int $uid
     * @param string $date 2022-01-01
     * @return bool
     * @throws \Exception
     */
    public function getUserIsSignIn(int $activeFlag, int $uid, string $date)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_sign_in', $activeFlag . ':' . $uid)
            ->getCache([$date], 'user_is_sign_in');
        return $data ? true : false;
    }

    /**
     * 获取当天用户是否分享
     * @param int $activeFlag
     * @param int $uid
     * @param string $date 2022-01-01
     * @return bool
     * @throws \Exception
     */
    public function getUserIsShare(int $activeFlag, int $uid, string $date)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_share', $activeFlag . ':' . $uid)
            ->getCache([$date], 'user_is_share');
        return $data ? true : false;
    }

    /**
     * 签到总天数
     * @param int $activeFlag
     * @param int $uid
     * @return int
     * @throws \Exception
     */
    public function getUserSignInDay(int $activeFlag, int $uid)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_is_sign_in', $activeFlag . ':' . $uid)
            ->getCache([], 'user_is_sign_in');

        return $data ? count($data) : 0;
    }

    /**
     * 用户签到
     */
    public function userSignIn(int $activeFlag, int $uid, int $date)
    {
        $yearActiveTextModel = new ZlYearActiveText();
        $textNum = 2;
        switch ($activeFlag)
        {
            case 202251:
                $this->set51textScale();
                $textNum = 1;
                break;
            case 2023:
                $this->set2023textScale();
                break;
            case 2024:
                $this->set2024textScale();
                break;
            default:
        }
        // 获取用户字信息
        $userTextInfo = $yearActiveTextModel->getUserTextInfo($activeFlag, $uid);
        // 获取 当前签到获得的字
        $textData = $this->getText($textNum);

        $changeData = [];
        foreach ($textData as $textItem)
        {
            if (isset($userTextInfo[$textItem]))
            {
                $changeData[] = [
                    'id' => $userTextInfo[$textItem]['id'],
                    'text' => $userTextInfo[$textItem]['text'],
                    'num' => $userTextInfo[$textItem]['num'] + 1,
                    'change_num' => 1,
                ];
            } else
            {
                $changeData[] = [
                    'text' => $textItem,
                    'num' => 1,
                    'change_num' => 1,
                ];
            }
        }
        // 更新用户字信息
        $res = $yearActiveTextModel->userChangeTextInfo($activeFlag, $uid, $changeData, 1);
        if ($res)
        {
            // 记录 签到 情况
            CacheManage::getInstance()->setCacheConfig($this->cacheModel)
                ->setCacheKeyDetail('user_is_sign_in', $activeFlag . ':' . $uid)
                ->setCache([$date => 1], 'user_is_sign_in', false);

            return [
                'status' => true,
                'text_info' => $textData,
            ];
        } else
        {
            return [
                'status' => false,
                'text_info' => [],
            ];
        }
    }

    /**
     * 用户分享
     */
    public function userShare(int $activeFlag, int $uid, int $date)
    {
        $yearActiveTextModel = new ZlYearActiveText();
        // 获取用户字信息
        $userTextInfo = $yearActiveTextModel->getUserTextInfo($activeFlag, $uid);
        switch ($activeFlag)
        {
            case 202251:
                $this->set51textScale();
                break;
            case 2023:
                $this->set2023textScale();
                break;
            case 2024:
                $this->set2024textScale();
                break;
            default:
        }
        // 获取 当前签到获得的字
        $textData = $this->getText(1);

        $changeData = [];
        foreach ($textData as $textItem)
        {
            if (isset($userTextInfo[$textItem]))
            {
                $changeData[] = [
                    'id' => $userTextInfo[$textItem]['id'],
                    'text' => $userTextInfo[$textItem]['text'],
                    'num' => $userTextInfo[$textItem]['num'] + 1,
                    'change_num' => 1,
                ];
            } else
            {
                $changeData[] = [
                    'text' => $textItem,
                    'num' => 1,
                    'change_num' => 1,
                ];
            }
        }
        // 更新用户字信息
        $res = $yearActiveTextModel->userChangeTextInfo($activeFlag, $uid, $changeData, 5);
        if ($res)
        {
            // 记录 签到 情况
            CacheManage::getInstance()->setCacheConfig($this->cacheModel)
                ->setCacheKeyDetail('user_is_share', $activeFlag . ':' . $uid)
                ->setCache([$date => 1], 'user_is_share', false);

            return [
                'status' => true,
                'text_info' => $textData,
            ];
        } else
        {
            return [
                'status' => false,
                'text_info' => [],
            ];
        }
    }

    /**
     * 随机 抽取对应的 字
     * @param int $textNum 获得几个不一样的字
     * @return array
     * @throws \Exception
     */
    protected function getText(int $textNum)
    {
        $resText = [];
        do
        {
            $num = random_int(1, 10000);
            $resScale = 0;
            foreach ($this->textScale as $text => $scale)
            {
                $resScale = $resScale + $scale * 10000;
                if ($num <= $resScale)
                {
                    if (!in_array($text, $resText, true))
                    {
                        $resText[] = (string)$text;
                    }
                    break;
                }
            }
        } while (count($resText) < $textNum);

        return $resText;
    }

    /**
     * 获取用户收到的字 信息
     * @param int $activeFlag
     * @param int $uid
     * @return array|bool|string
     * @throws \Exception
     */
    public function getUserNewTextInfo(int $activeFlag, int $uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_new_text', $activeFlag . ':' . $uid)
            ->getCache([], 'user_new_text');
    }

    /**
     * 用户收到 新的字 增加
     * @param int $activeFlag
     * @param int $uid
     * @param string $text
     * @param int $inc
     * @throws \Exception
     */
    public function userNewTextAdd(int $activeFlag, int $uid, string $text, int $inc = 1)
    {
        CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_new_text', $activeFlag . ':' . $uid)
            ->setCacheInc($text, 'user_new_text', $inc);
    }

    /**
     * 用户收到 新的字 知晓 数量清零0
     * @param int $activeFlag
     * @param int $uid
     * @param string $text
     * @throws \Exception
     */
    public function userNewTextNumChange(int $activeFlag, int $uid, string $text)
    {
        CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('user_new_text', $activeFlag . ':' . $uid)
            ->setCache([$text => 0], 'user_new_text');
    }

    /**
     * 设置51活动集字配置
     */
    public function set51textScale()
    {
        $this->textScale = [
            '光荣' => 0.04,
            '劳动' => 0.32,
            '勤奋' => 0.32,
            '果实' => 0.32,
        ];
    }

    /**
     * 设置2023春节活集字配置
     */
    public function set2023textScale()
    {
        $this->textScale = [
            '2023' => 0.03,
            '兔' => 0.32,
            '个' => 0.31,
            '好' => 0.03,
            '运' => 0.31,
        ];
    }

    /**
     * 设置2024春节活集字配置
     */
    public function set2024textScale()
    {
        $this->textScale = [
            '2024' => 0.01,
            '愉' => 0.25,
            '跃' => 0.25,
            '龙' => 0.25,
            '门' => 0.24,
        ];
    }
}
